package com.star.star.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.star.star.dto.StarAlbumPageWxuserDTO;
import com.star.star.dto.StarAlbumSaveWxuserDTO;
import com.star.star.entity.Star;
import com.star.star.service.StarAlbumArtService;
import com.star.star.service.StarArtService;
import com.star.star.vo.StarAlbumDetailWxuserVO;
import com.star.star.dto.StarAlbumUpdateWxuserDTO;
import com.star.star.entity.StarAlbum;
import com.star.star.mapper.StarAlbumMapper;
import com.star.star.service.StarAlbumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.star.service.StarService;
import com.star.star.struct.StarAlbumStruct;
import com.star.star.vo.StarAlbumPageWxuserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 艺人专辑 服务实现类
 *
 * @author yangzj
 */
@Service
public class StarAlbumServiceImpl extends ServiceImpl<StarAlbumMapper, StarAlbum> implements StarAlbumService {

    @Autowired
    private StarService starService;
    @Autowired
    private StarArtService starArtService;
    @Autowired
    private StarAlbumArtService starAlbumArtService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveWxuser(StarAlbumSaveWxuserDTO dto, Long userId) {
        Star star = Assert.notNull(starService.getByUserId(userId), "您还不是艺人");
        this.check(star.getId(), userId);
        this.save(StarAlbumStruct.INSTANCE.to(dto).setStarId(star.getId()));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateWxuser(StarAlbumUpdateWxuserDTO dto, Long userId) {
        Star star = Assert.notNull(starService.getByUserId(userId), "您还不是艺人");
        this.check(star.getId(), userId);
        StarAlbum starAlbum = StarAlbumStruct.INSTANCE.to(dto);
        this.update(starAlbum, Wrappers.<StarAlbum>lambdaUpdate()
                .eq(StarAlbum::getId, starAlbum.getId())
                .set(StarAlbum::getDescription, dto.getDescription()));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIdWxuser(Long id, Long userId) {
        StarAlbum starAlbum = Assert.notNull(this.getById(id), "专辑不存在");
        this.check(starAlbum.getStarId(), userId);
        this.removeById(id);
        // 删除后，所有关联的歌曲，将归属为单曲
        List<Long> artIds = starAlbumArtService.listArtId(id);
        if(artIds.isEmpty()){
            return;
        }
        starAlbumArtService.deleteWxuserByArtIds(artIds);
        // 变更为单曲
        starArtService.updateBatchIsAlbumWxuser(artIds, false);
    }

    @Override
    public IPage<StarAlbumPageWxuserVO> pageWxuser(StarAlbumPageWxuserDTO dto) {
        return baseMapper.pageWxuser(dto);
    }

    @Override
    public StarAlbumDetailWxuserVO getDetailWxuser(Long id, Long userId) {
        return baseMapper.getDetailWxuser(id, userId);
    }

    /**
     * 检查
     *
     * @param starId 明星ID
     * @param userId 用户ID
     */
    private void check(Long starId, Long userId){
        Assert.isTrue(starService.exists(starId, userId), "艺人专辑不存在");
    }
}
